﻿using UnityEngine;
using System.Collections;

public class ClicPlateau : MonoBehaviour {
	//


	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void OnMouseDown(){

		// Raycast
		Vector3 pos = new Vector3(-100,-100,-100);

		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		if (Physics.Raycast(ray, out hit ,10000)){

			pos = hit.point;

		}

		// If a raycast was found, check input
		if(pos != new Vector3(-100,-100,-100)){

			// Find the manager
			ManagerHabitant ManageHab = GameObject.FindObjectOfType<ManagerHabitant>();

			if(Input.GetMouseButtonDown(0) && ManageHab.Habitants.Count > 0){

				float rng = Random.Range(-200, 200);
				// Random check
				if(rng+ManageHab.Habitants[0].MoodLevel > 0){

					ManageHab.Habitants[0].navMeshAgent.SetDestination(pos);

				}

				

			}
			if(Input.GetMouseButtonDown(0) && Input.GetMouseButtonDown(1)){
				// Foreach villager

				System.Random rng = new System.Random ();

				//Debug.Log("ClicPlateau.OnMouseDown - Clic 1/2 foreach");

				foreach(Habitant h in ManageHab.Habitants){



					float rngRes =(float) rng.Next(-200,200);
					// Random check
					if(rngRes+h.MoodLevel > 0){
						
						h.navMeshAgent.SetDestination(pos);

					}

				}

			}


		}

	}

}
